﻿using System;
using System.IO.IsolatedStorage;
using System.Diagnostics;
using System.Collections.Generic;
using System.Windows;
using System.ComponentModel;
using Microsoft.Phone.Shell;

namespace Jive.Settings
{
    public class AppSettings : INotifyPropertyChanged
    {
        private static AppSettings _instance;
        public static AppSettings AppSettingsInstance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new AppSettings();
                    return _instance;
                }
                else
                    return _instance;
            }
        }
        // Our settings
        IsolatedStorageSettings settings;
        // The key names of our settings
        const string IsLockscreenDisabledSettingKeyName = "DisableLockscreenSetting";
        const string StoreMessagesWhileOfflineSettingKeyName = "StoreMessagesWhileOfflineSetting";
        const string OfflineMessageAgeLimitSettingKeyName = "OfflineMessageAgeLimitSetting";
        const string OfflineMessageCountLimitSettingKeyName = "OfflineMessageCountLimitSetting";
        const string IsOfflinePmToastEnabledSettingKeyName = "IsOfflinePmToastEnabledSetting";
        const string IsOfflineGroupToastEnabledSettingKeyName = "IsOfflineGroupToastEnabledSetting";
        const string CustomContactEmoticonsKeyName = "CustomContactEmoticons";
        const string CustomEmoticonsKeyName = "CustomEmoticons";
        const string ServerListSettingKeyName = "ServerListSetting";
        const string ServerSettingKeyName = "ServerSetting";
        const string UsernameSettingKeyName = "UsernameSetting";
        const string PasswordSettingKeyName = "PasswordSettings";
        const string EnableToastNotificationsKeyName = "EnableToastNotifications";
        const string EnableTileNotificationsKeyName = "EnableTileNotifications";
        const string HostnameCacheKeyName = "HostnameCache";
        const string RecentlyUsedDefaultEmoticonsKeyName = "RecentlyUsedDefaultEmoticons";
        const string RecentlyUsedCustomEmoticonsKeyName = "RecentlyUsedCustomEmoticons";
        const string LastOpenedEmoticonPageIndexKeyName = "LastOpenedEmoticonPageIndex";
        //setting defaults
        const string ServerSettingDefault = null;
        const string UsernameSettingDefault = "user@domain.com";
        const string PasswordSettingDefault = "";
        private Dictionary<string,Dictionary<string,string>> CustomContactEmoticonsDefault = new Dictionary<string,Dictionary<string,string>>();
        private Dictionary<string, string> CustomEmoticonsDefault = new Dictionary<string, string>();
        private List<string> ServerListSettingDefault = null;//new List<string>();
        const bool EnableToastNotificationDefault = true;
        const bool EnableTileNotificationDefault = true;
        const bool IsLockscreenDisabledSettingDefault = false;
        const bool StoreMessagesWhileOfflineSettingDefault = true;
        const bool IsOfflineGroupToastEnabledSettingDefault = false;
        const bool IsOfflinePmToastEnabledSettingDefault = true;
        private Dictionary<string, string> HostnameCacheDefault = new Dictionary<string,string>();
        const int OfflineMessageAgeLimitSettingDefault = 6;
        const int OfflineMessageCountLimitSettingDefault = 150;
        private Dictionary<string[], int> RecentlyUsedDefaultEmoticonsDefault = new Dictionary<string[], int>();
        private Dictionary<string, int> RecentlyUsedCustomEmoticonsDefault = new Dictionary<string,int>();
        const int LastOpenedEmoticonPageIndexDefault = 0;
        /// <summary>
        /// Constructor that gets the application settings.
        /// </summary>
        private AppSettings()
        {
            // Get the settings for this application.
            if (!DesignerProperties.IsInDesignTool)
            {
                settings = IsolatedStorageSettings.ApplicationSettings;
                Debug.WriteLine("Created instance of AppSettings");
            }
        }
        //public static AppSettings GetInstance()
        //{
        //    if (_instance == null)
        //        return _instance = new AppSettings();
        //    else
        //        return _instance;
        //}
        #region INotifyPropertyChanged implementation

        public event PropertyChangedEventHandler PropertyChanged;
        protected void RaisePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion
        /// <summary>
        /// Update a setting value for our application. If the setting does not
        /// exist, then add the setting.
        /// </summary>
        /// <param name="Key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool AddOrUpdateValue(string Key, Object value)
        {
            if (DesignerProperties.IsInDesignTool)
                return false;

            bool valueChanged = false;

            // If the key exists
            if (settings.Contains(Key))
            {
                // If the value has changed
                if (settings[Key] != value)
                {
                    // Store the new value
                    settings[Key] = value;
                    valueChanged = true;
                }
            }
            // Otherwise create the key.
            else
            {
                settings.Add(Key, value);
                valueChanged = true;
            }
            return valueChanged;
        }
        /// <summary>
        /// Get the current value of the setting, or if it is not found, set the 
        /// setting to the default setting.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="Key"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public T GetValueOrDefault<T>(string Key, T defaultValue)
        {
            T value;

            // If the key exists, retrieve the value.
            if (settings!=null&& settings.Contains(Key))
            {

                value = (T)settings[Key];

            }
            // Otherwise, use the default value.
            else
            {
                value = defaultValue;
            }
            return value;
        }

        /// <summary>
        /// Save the settings.
        /// </summary>
        public void Save()
        {
            settings.Save();
        }

        /// <summary>
        /// Property to get and set a Toast notification Setting.
        /// </summary>
        public bool IsToastNotificationEnabledSetting
        {
            get
            {
                return GetValueOrDefault<bool>(EnableToastNotificationsKeyName, EnableToastNotificationDefault);
            }
            set
            {
                if (AddOrUpdateValue(EnableToastNotificationsKeyName, value))
                {
                    Save();
                    RaisePropertyChanged(" IsToastNotificationEnabledSetting");
                    //(App.Current as App).RegisterPush(true, (App.Current as App).AppSettings.IsToastNotificationEnabledSetting);
                }
            }
        }
        /// <summary>
        /// roperty to get and set disable lock screen setting.
        /// </summary>
        public bool IsLockscreenDisabledSetting
        {
            get
            {
                return GetValueOrDefault<bool>(IsLockscreenDisabledSettingKeyName, IsLockscreenDisabledSettingDefault);
            }
            set
            {
                if (AddOrUpdateValue(IsLockscreenDisabledSettingKeyName, value))
                {
                    Save();
                    RaisePropertyChanged("IsLockscreenDisabledSetting");
                    //App.SetLockscreenState(value);
                }
            }
        }

        /// <summary>
        /// Property to get and set a Tile notification Setting.
        /// </summary>
        public bool IsTileNotificationEnabledSetting
        {
            get
            {
                return GetValueOrDefault<bool>(EnableTileNotificationsKeyName, EnableTileNotificationDefault);
            }
            set
            {
                if (AddOrUpdateValue(EnableTileNotificationsKeyName, value))
                {
                    Save();
                    RaisePropertyChanged("IsTileNotificationEnabledSetting");
                   // (App.Current as App).RegisterPush(value, IsToastNotificationEnabledSetting);
                }
            }
        }

        /// <summary>
        /// Property to get and set a Server Setting.
        /// </summary>
        public string ServerSetting
        {
            get
            {
                return GetValueOrDefault<string>(ServerSettingKeyName, ServerSettingDefault);
            }
            set
            {
                if (AddOrUpdateValue(ServerSettingKeyName, value))
                {
                    Save();
                    RaisePropertyChanged("ServerSetting");
                }
            }
        }
        /// <summary>
        /// Property to get and set a Username Setting.
        /// </summary>
        public string UsernameSetting
        {
            get
            {
                return GetValueOrDefault<string>(UsernameSettingKeyName, UsernameSettingDefault);
            }
            set
            {
                if (AddOrUpdateValue(UsernameSettingKeyName, value))
                {
                    Save();
                    RaisePropertyChanged("UsernameSetting");
                }
            }
        }

        /// <summary>
        /// Property to get and set a Password Setting.
        /// </summary>
        public string PasswordSetting
        {
            get
            {
                return GetValueOrDefault<string>(PasswordSettingKeyName, PasswordSettingDefault);
            }
            set
            {
                if (AddOrUpdateValue(PasswordSettingKeyName, value))
                {

                    Save();
                    RaisePropertyChanged("PasswordSetting");
                }
            }
        }
        /// <summary>
        /// Property to get and set a Hostname Cache Setting.
        /// </summary>
        public Dictionary<string, string> HostnameCache
        {
            get
            {
                return GetValueOrDefault<Dictionary<string, string>>(HostnameCacheKeyName, HostnameCacheDefault);
            }
            set
            {
                if (AddOrUpdateValue(HostnameCacheKeyName, value))
                {
                    Save();
                    RaisePropertyChanged("HostnameCache");
                }
            }
        }
        ///// <summary>
        ///// Property to get and set a Hostname Cache Setting.
        ///// </summary>
        //public DateTime LastTaskRun
        //{
        //    get
        //    {
        //        return GetValueOrDefault<DateTime>(LastTaskRunSettingKeyName, LastTaskRunSettingDefault);
        //    }
        //    set
        //    {
        //        if (AddOrUpdateValue(LastTaskRunSettingKeyName, value))
        //        {
        //            Save();
        //        }
        //    }
        //}
        /// <summary>
        /// Property to get and set a server list Setting.
        /// </summary>
        public List<string> ServerListSetting
        {
            get
            {
                return GetValueOrDefault<List<string>>(ServerListSettingKeyName, ServerListSettingDefault);
            }
            set
            {
                if (AddOrUpdateValue(ServerListSettingKeyName, value))
                {
                    Save();
                    RaisePropertyChanged("ServerListSetting");
                }
            }
        }

        public Dictionary<string, Dictionary<string,string>> CustomContactEmoticons
        {
            get
            {
                return GetValueOrDefault<Dictionary<string,Dictionary<string,string>>>(CustomContactEmoticonsKeyName, CustomContactEmoticonsDefault);
            }
            set
            {
                if (AddOrUpdateValue(CustomContactEmoticonsKeyName, value))
                {
                    Save();
                    RaisePropertyChanged("CustomContactEmoticons");
                }
            }
        }
        public Dictionary<string, string> CustomEmoticons
        {
            get
            {
                return GetValueOrDefault<Dictionary<string, string>>(CustomEmoticonsKeyName, CustomEmoticonsDefault);
            }
            set
            {
                if (AddOrUpdateValue(CustomEmoticonsKeyName, value))
                {
                    Save();
                    RaisePropertyChanged("CustomEmoticons");
                }
            }
        }

        public bool StoreMessagesWhileOfflineSetting
        {
            get
            {
                return GetValueOrDefault<bool>(StoreMessagesWhileOfflineSettingKeyName, StoreMessagesWhileOfflineSettingDefault);
            }
            set
            {
                if (AddOrUpdateValue(StoreMessagesWhileOfflineSettingKeyName, value))
                {
                    Save();
                    RaisePropertyChanged("StoreMessagesWhileOfflineSetting");
                }
            }
        }
        public int OfflineMessageCountLimitSetting
        {
            get
            {
                return GetValueOrDefault<int>(OfflineMessageCountLimitSettingKeyName, OfflineMessageCountLimitSettingDefault);
            }
            set
            {
                if (AddOrUpdateValue(OfflineMessageCountLimitSettingKeyName, value))
                {
                    Save();
                    RaisePropertyChanged("OfflineMessageCountLimitSetting");
                }
            }
        }
        public int OfflineMessageAgeLimitSetting
        {
            get
            {
                return GetValueOrDefault<int>(OfflineMessageAgeLimitSettingKeyName, OfflineMessageAgeLimitSettingDefault);
            }
            set
            {
                if (AddOrUpdateValue(OfflineMessageAgeLimitSettingKeyName, value))
                {
                    Save();
                    RaisePropertyChanged("OfflineMessageAgeLimitSetting");
                }
            }
        }
        public Dictionary<string[], int> RecentlyUsedDefaultEmoticons
        {
            get
            {
                return GetValueOrDefault<Dictionary<string[], int>>(RecentlyUsedDefaultEmoticonsKeyName, RecentlyUsedDefaultEmoticonsDefault);
            }
            set
            {
                if (AddOrUpdateValue(RecentlyUsedDefaultEmoticonsKeyName, value))
                {
                    Save();
                    RaisePropertyChanged("RecentlyUsedDefaultEmoticons");
                }
            }
        }
        public Dictionary<string, int> RecentlyUsedCustomEmoticons
        {
            get
            {
                return GetValueOrDefault<Dictionary<string, int>>(RecentlyUsedCustomEmoticonsKeyName, RecentlyUsedCustomEmoticonsDefault);
            }
            set
            {
                if (AddOrUpdateValue(RecentlyUsedCustomEmoticonsKeyName, value))
                {
                    Save();
                    RaisePropertyChanged("RecentlyUsedCustomEmoticons");
                }
            }
        }
        public int LastOpenedEmoticonPageIndex
        {
            get
            {
                return GetValueOrDefault<int>(LastOpenedEmoticonPageIndexKeyName, LastOpenedEmoticonPageIndexDefault);
            }
            set
            {
                if (AddOrUpdateValue(LastOpenedEmoticonPageIndexKeyName, value))
                {
                    Save();
                    RaisePropertyChanged("LastOpenedEmoticonPageIndex");
                }
            }
        }
        public bool OfflinePmToastEnabledSetting
        {
            get
            {
                return GetValueOrDefault<bool>(IsOfflinePmToastEnabledSettingKeyName, IsOfflinePmToastEnabledSettingDefault);
            }
            set
            {
                if (AddOrUpdateValue(IsOfflinePmToastEnabledSettingKeyName, value))
                {
                    Save();
                    RaisePropertyChanged(" OfflinePmToastEnabledSetting");
                }
            }
        }
        public bool OfflineGroupToastEnabledSetting
        {
            get
            {
                return GetValueOrDefault<bool>(IsOfflineGroupToastEnabledSettingKeyName, IsOfflineGroupToastEnabledSettingDefault);
            }
            set
            {
                if (AddOrUpdateValue(IsOfflineGroupToastEnabledSettingKeyName, value))
                {
                    Save();
                    RaisePropertyChanged("OfflineGroupToastEnabledSetting");
                }
            }
        }
    }
}
